Learning GraphQL by Eve Porcello and Alex Banks

Learning GraphQL by Eve Porcello and Alex Banks

Author:Eve Porcello and Alex Banks
Language: eng
Format: mobi
Publisher: O'Reilly Media, Inc.
Published: 2018-08-07T16:00:00+00:00


npm remove apollo-server

Then, let’s install Apollo Server Express and Express:

npm install apollo-server-express express

Express

Express is by far one of the most popular projects in the Node.js ecosystem. It allows you to set up a Node.js web application quickly and efficiently.

From here, we can refactor our index.js file. We’ll start by changing the require statement to include apollo-server-express. Then we’ll include express:

// 1. Require `apollo-server-express` and `express` const { ApolloServer } = require('apollo-server-express') const express = require('express') ... // 2. Call `express()` to create an Express application var app = express() const server = new ApolloServer({ typeDefs, resolvers }) // 3. Call `applyMiddleware()` to allow middleware mounted on the same path server.applyMiddleware({ app }) // 4. Create a home route app.get('/', (req, res) => res.end('Welcome to the PhotoShare API')) // 5. Listen on a specific port app.listen({ port: 4000 }, () => console.log(`GraphQL Server running @ http://localhost:4000${server.graphqlPath}`) )

By including Express, we can take advantage of all of the middleware functions provided to us by the framework. To incorporate this into the server, we just need to call the express function, call applyMiddleware, and then we can set up a custom route. Now when we visit http://localhost:4000, we should see a page that reads “Welcome to the PhotoShare API”. This is a placeholder for now.

Next, we want to set up a custom route for the GraphQL Playground to run at http://localhost:4000/playground. We can do so by installing a helper package from npm. First, we need to install the package, graphql-playground-middleware-express:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.